package org.cupk2.wdap.controller;

import jakarta.validation.Valid;
import org.cupk2.files.service.ResourceFileServiceInter;
import org.cupk2.tools.web.dto.IdDto;
import org.cupk2.wdap.convertor.utils.WdapFlowRunner;
import org.cupk2.wdap.domain.WdapTask;
import org.cupk2.wdap.dto.WdapTaskDto;
import org.cupk2.wdap.service.WdapFlowServiceInter;
import org.cupk2.wdap.service.WdapTaskServiceInter;
import org.cupk2.tools.bean.CupkBeanUtils;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataQueryDto;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBSort;
import org.cupk2.tools.web.CupkResponseCode;
import org.cupk2.tools.web.CupkResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 转换任务
 *
 * @author cbtg自动生成  2025-1-25 9:21:04
 */
@RestController
@RequestMapping("/api/wdaptask")
public class WdapTaskController {
    @Autowired
    private WdapTaskServiceInter wdapTaskServiceImpl;
    @Autowired
    private WdapFlowServiceInter wdapFlowServiceImpl;
    @Autowired
    private WdapFlowRunner wdapFlowRunner;
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    /**
     * 条件查询
     *
     * @param query
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.query')")
    @PostMapping("/query")
    public CupkResponseResult queryAll(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.addDefaultSort(new DBSort("ETIME", DBSort.SORT_TYPE.DESC));
        dataQuery.setRuleAsSql("default", "and (B.TITLE like '%?%' OR A.FILEID like '%?%')");
        DataResult result = wdapTaskServiceImpl.searchWdapTask(dataQuery);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
    }

    /**
     * 添加数据
     *
     * @param wdapTaskDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.add')")
    @PostMapping
    public CupkResponseResult addSubmit(@Valid @RequestBody WdapTaskDto wdapTaskDto) {
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**
     * 修改数据
     *
     * @param id
     * @param wdapTaskDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.edit')")
    @PutMapping("/{id}")
    public CupkResponseResult editSubmit(@PathVariable String id, @Valid @RequestBody WdapTaskDto wdapTaskDto) {
        wdapTaskDto.setId(id);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.del')")
    @DeleteMapping("/{id}")
    public CupkResponseResult delSubmit(@PathVariable String id) {
        wdapTaskServiceImpl.delWdapTask(id);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, id);
    }


    /**
     * 启动转换任务
     *
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.run')")
    @PostMapping("/run")
    public CupkResponseResult runTask(@RequestBody IdDto id) {
        wdapTaskServiceImpl.initStateToWait(id.getId());
        wdapFlowRunner.startRunTasks();
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, id);
    }

    /**
     * 批量删除数据
     *
     * @param ids 要删除的记录ID列表
     * @return 操作结果
     */
    @PreAuthorize("@cupkAction.has('wdapTask.del')")
    @DeleteMapping("/batch")
    public CupkResponseResult batchDelSubmit(@RequestBody List<String> ids) {
        for (String id : ids) {
            wdapTaskServiceImpl.delWdapTask(id);
        }
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**
     * 数据浏览
     *
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('wdapTask.info')")
    @GetMapping("/{id}")
    public CupkResponseResult info(@PathVariable String id) {
        WdapTask wdapTask = wdapTaskServiceImpl.getWdapTaskById(id);

        WdapTaskDto dto = new WdapTaskDto();
        dto = CupkBeanUtils.copyProperties(wdapTask, new WdapTaskDto());
        dto.setExPath(resourceFileServiceImpl.getExDirPath(resourceFileServiceImpl.getResourceFileById(wdapTask.getFileid())).getPath());
        return new CupkResponseResult(CupkResponseCode.SUCESS, dto);
    }
}
